// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IdleWork.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Idle work that sleeps for
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Threading;
using AzureContrib.WindowsAzure.Diagnostics;
using AzureContrib.WindowsAzure.Properties;

namespace AzureContrib.WindowsAzure.WorkServices
{
	/// <summary>
	/// Idle work that sleeps for
	/// </summary>
	internal class IdleWork : IWork
	{
		/// <summary>
		/// Gets or sets he default numer of seconds to sleep
		/// </summary>
		public const int DefaultSecondsToSleep = 10;

		private readonly ITrace trace;

		/// <summary>
		/// Initializes a new instance of the <see cref="IdleWork"/> class. 
		/// </summary>
		/// <param name="trace">
		/// The trace.
		/// </param>
		public IdleWork(ITrace trace)
		{
			Assumes.NotNull(trace, "trace");
			this.trace = trace;
			SecondsToSleep = DefaultSecondsToSleep;
		}

		/// <summary>
		/// Gets or sets the seconds to sleep with this idle task. Default is 10 seconds.
		/// </summary>
		public int SecondsToSleep { get; set; }

		/// <summary>
		/// Gets a value indicating whether the <see cref="IWorkItem"/> has any work to perform at the moment.
		/// </summary>
		/// <remarks>Allways true.</remarks>
		public bool HasWork
		{
			get { return true; }
		}

		public int Order
		{
			get { throw new NotSupportedException("Idle work is not supposed to be orered."); }
		}

		public WorkType WorkType
		{
			get { return WorkType.Run; }
		}

		public string Name
		{
			get { return "Idle Work"; }
		}

		public string Description
		{
			get { return "A work task that does nothing other than wait. I am an instance that waits for {0} seconds.".F(SecondsToSleep); }
		}

		/// <summary>
		/// Do the actual work of the <see cref="IWorkItem"/> which is a <see cref="Thread.Sleep(int)"/> for <see cref="SecondsToSleep"/> seconds.
		/// </summary>
		public void DoWork()
		{
			trace.Information(Strings.NoWaitingWorkItems, SecondsToSleep);
			Thread.Sleep(new TimeSpan(0, 0, 0, SecondsToSleep));
		}
	}
}